/**
 * Copyright (C), 2021, 因特驰
 * FileName: LoginController
 * Author:   李晋武
 * Date:     2021/7/17 14:26
 * Description:
 * History:
 * 李晋武          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.esc.controller;

import com.esc.model.Buyer;
import com.esc.model.Seller;
import com.esc.service.BuyerService;
import com.esc.service.SellerService;
import com.esc.util.CheckImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 〈买车用户、卖车用户登录功能〉<br>
 * 〈〉
 *
 * @author Yun
 * @create 2021/7/17
 * @since 1.0.0
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private BuyerService buyerService;
    @Autowired
    private SellerService sellerService;

    //去登录
    @RequestMapping("toLogin")
    public String toLogin(){
        return "user/login";
    }
    //加载验证码
    @RequestMapping("imgcode")
    public String imgcode(HttpServletRequest request, HttpServletResponse response){
        String checkImgPath = "";
        try {
            checkImgPath = CheckImgUtil.checkImg(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkImgPath;
    }

    //去注册
    @RequestMapping("toRegister")
    public String toRegister(){
        return "user/register";
    }

    //校验用户是否存在
    @RequestMapping("isNullWhitPhone")
    @ResponseBody
    public String isNullWhitPhone(String userPhone,Integer userType){
        if (userType==1){
            Buyer buyer = buyerService.queryBuyerByPhone(userPhone);
            if (buyer==null){
                return "0";
            }
        }else {
            Seller seller = sellerService.querySellerByPhone(userPhone);
            if (seller==null){
                return "0";
            }
        }
        return "1";
    }
}